package com.example.testpic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.testpic.BitmapCache.NativeImageCallBack;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageGridAdapter extends BaseAdapter {

	private TextCallback textcallback = null;
	final String TAG = getClass().getSimpleName();
	Activity act;
	List<ImageItem> dataList;
	BitmapCache cache;
	private GridView gridView;
	private Bitmap bitmap;

	public static interface TextCallback {
		public void onListen(int count);
	}

	public void setTextCallback(TextCallback listener) {
		textcallback = listener;
	}

	public ImageGridAdapter(Activity act, List<ImageItem> list, GridView gridView) {
		this.act = act;
		dataList = list;
		this.gridView = gridView;
		cache = BitmapCache.getCacheInstance(act);
	}

	@Override
	public int getCount() {
		int count = 0;
		if (dataList != null) {
			count = dataList.size();
		}
		return count;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	class Holder {
		private ImageView iv;
		private ImageView selected;
		private TextView text;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final Holder holder;

		if (convertView == null) {
			holder = new Holder();
			convertView = View.inflate(act, R.layout.item_image_grid, null);
			holder.iv = (ImageView) convertView.findViewById(R.id.image);
			holder.selected = (ImageView) convertView
					.findViewById(R.id.isselected);
			holder.text = (TextView) convertView
					.findViewById(R.id.item_image_grid_text);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		final ImageItem item = dataList.get(position);

		holder.iv.setTag(item.imagePath);
		bitmap = cache.loadBitmap(holder.iv, item.imagePath,
				new NativeImageCallBack() {

					@Override
					public void onImageLoader(Bitmap bitmap, String path) {
						// TODO Auto-generated method stub
						ImageView imageView = (ImageView) gridView
								.findViewWithTag(path);
						if (imageView != null && bitmap != null) {
							imageView.setImageBitmap(bitmap);
						}
					}

				});
		if (bitmap != null) {
			holder.iv.setImageBitmap(bitmap);
		}

		if(AlbumListActivity.map.containsKey(item.imagePath)){
			holder.selected.setImageResource(R.drawable.icon_data_select);
			holder.text.setBackgroundResource(R.drawable.bgd_relatly_line);
		} else {
			holder.selected.setImageResource(-1);
			holder.text.setBackgroundColor(0x00000000);
		}
		holder.iv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String path = dataList.get(position).imagePath;
				item.isSelected = !AlbumListActivity.map.containsKey(path);
				if (item.isSelected) {
					holder.selected
							.setImageResource(R.drawable.icon_data_select);
					holder.text
							.setBackgroundResource(R.drawable.bgd_relatly_line);
					AlbumListActivity.imageSelectedTotal++;
					if (textcallback != null)
						textcallback.onListen(AlbumListActivity.imageSelectedTotal);
					AlbumListActivity.map.put(path, path);
					AlbumListActivity.pathList.add(path);
				} else if (!item.isSelected) {
					holder.selected.setImageResource(-1);
					holder.text.setBackgroundColor(0x00000000);
					AlbumListActivity.imageSelectedTotal--;
					if (textcallback != null)
						textcallback.onListen(AlbumListActivity.imageSelectedTotal);
					AlbumListActivity.map.remove(path);
					AlbumListActivity.pathList.remove(AlbumListActivity.pathList.indexOf(path));
				}
			}
		});

		return convertView;
	}
}
